//初始化函数
$(function() {
    //平台占比输入框失去焦点触发事件
    $("#platformScale").on('blur', function() {
        var platformScale = $("#platformScale").val();
        var authorScale = 100 - parseInt(platformScale);
        $("#authorScale").val(authorScale)
    });

    //作者占比输入框失去焦点触发事件
    $("#authorScale").on('blur', function() {
        var authorScale = $("#authorScale").val();
        var platformScale = 100 - parseInt(authorScale);
        $("#platformScale").val(platformScale)
    });
});

//保存
function save(){

    //收益类型
    if($("#utilityType").val() == ""){
        $("#utilityTypeSelect").tips({
            side: 3,
            msg: '选择收益类型',
            bg: '#AE81FF',
            time: 2
        });
        $("#utilityType").focus();
        return false;
    }

    //平台占比
    if($("#platformScale").val()==""){
        $("#platformScale").tips({
            side:3,
            msg:'请输入平台占比',
            bg:'#AE81FF',
            time:2
        });
        $("#platformScale").focus();
        return false;
    }else if(!$.fn.isPositiveInteger($("#platformScale").val())){
        $("#platformScale").tips({
            side:3,
            msg:'请输入正整数',
            bg:'#AE81FF',
            time:2
        });
        $("#platformScale").focus();
        return false;
    }else if(parseInt($("#platformScale").val()) > 100){
        $("#platformScale").tips({
            side:3,
            msg:'请输入小于等于100的正整数',
            bg:'#AE81FF',
            time:2
        });
        $("#platformScale").focus();
        return false;
    }else{
        $("#platformScale").val($.trim($("#platformScale").val()));
    }

    //作者占比
    if($("#authorScale").val()==""){
        $("#authorScale").tips({
            side:3,
            msg:'请输入作者占比',
            bg:'#AE81FF',
            time:2
        });
        $("#authorScale").focus();
        return false;
    }else if(!$.fn.isPositiveInteger($("#authorScale").val())){
        $("#authorScale").tips({
            side:3,
            msg:'请输入正整数',
            bg:'#AE81FF',
            time:2
        });
        $("#authorScale").focus();
        return false;
    }else if(parseInt($("#authorScale").val()) > 100){
        $("#authorScale").tips({
            side:3,
            msg:'请输入小于等于100的正整数',
            bg:'#AE81FF',
            time:2
        });
        $("#authorScale").focus();
        return false;
    }else{
        $("#authorScale").val($.trim($("#authorScale").val()));
    }

    var sum = parseInt($("#platformScale").val()) + parseInt($("#authorScale").val());
    if (sum != 100) {
        $("#authorScale").tips({
            side:3,
            msg:'平台占比与作者占比的和必须等于100%',
            bg:'#AE81FF',
            time:2
        });
        $("#authorScale").focus();
        return false;
    }

    $("#form").submit();
    $("#zhongxin").hide();
    $("#zhongxin2").show();
}

$(function() {
    //下拉框
    if(!ace.vars['touch']) {
        $('.chosen-select').chosen({allow_single_deselect:true});
        $(window)
            .off('resize.chosen')
            .on('resize.chosen', function() {
                $('.chosen-select').each(function() {
                    var $this = $(this);
                    $this.next().css({'width': $this.parent().width()});
                });
            }).trigger('resize.chosen');
        $(document).on('settings.ace.chosen', function(e, event_name, event_val) {
            if(event_name != 'sidebar_collapsed') return;
            $('.chosen-select').each(function() {
                var $this = $(this);
                $this.next().css({'width': $this.parent().width()});
            });
        });
        $('#chosen-multiple-style .btn').on('click', function(e){
            var target = $(this).find('input[type=radio]');
            var which = parseInt(target.val());
            if(which == 2) $('#form-field-select-4').addClass('tag-input-style');
            else $('#form-field-select-4').removeClass('tag-input-style');
        });
    }
});